package net.mihome.app.ui;

import java.util.ArrayList;
import java.util.List;

import net.mihome.app.AppContext;
import net.mihome.app.AppException;
import net.mihome.app.adapter.ListViewTweetAdapter;
import net.mihome.app.bean.Tweet;
import net.mihome.app.bean.TweetList;
import net.mihome.app.common.UIHelper;
import net.mihome.app.widget.PullToRefreshListView;
import net.mihome.app.widget.PullToRefreshListView.OnRefreshListener;
import net.mihome.app.R;
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

public class BigPaperViewActivity extends Activity {
	private ListViewTweetAdapter listViewTweetAdapter;
	private List<Tweet> tweetList=new ArrayList<Tweet>();
	private PullToRefreshListView pullToRefreshListView;
	private View progressView;
	private Handler handler;
	private AppContext appContext;
	private int uid;
	private String userName;
	private final static int MESSAGE_EXCEPTION=-1;
	private final static int SHOW_PROGRESS=305;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bigpaper_main);
		initView();
	}
	public void initView(){
		progressView=findViewById(R.id.user_center_head_progress);
		uid=getIntent().getIntExtra("his_id", -1);
		if(uid<0){
			UIHelper.showMsgDialog(this,R.string.tishi,R.string.uid_wuxiao,new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					BigPaperViewActivity.this.finish();
				}
			});
		}
		pullToRefreshListView=(PullToRefreshListView) findViewById(R.id.dazibao_view);
		listViewTweetAdapter=new ListViewTweetAdapter(this,tweetList,R.layout.tweet_listitem);
		pullToRefreshListView.setAdapter(listViewTweetAdapter);
		pullToRefreshListView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				handler.sendEmptyMessage(SHOW_PROGRESS);
				initData();
			}
		});
		appContext=(AppContext) getApplication();
		handler=new Handler(new Handler.Callback() {
			@Override
			public boolean handleMessage(Message arg0) {
				// TODO Auto-generated method stub
				switch (arg0.what) {
				case MESSAGE_EXCEPTION:
					UIHelper.showMsgDialog(BigPaperViewActivity.this,R.string.tishi,R.string.network_not_connected,new DialogInterface.OnClickListener() {
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							BigPaperViewActivity.this.finish();
						}
					});
					break;
				case SHOW_PROGRESS:
					progressView.setVisibility(View.VISIBLE);
					pullToRefreshListView.onRefreshComplete();
					break;
				default:
					listViewTweetAdapter=new ListViewTweetAdapter(BigPaperViewActivity.this,tweetList,R.layout.tweet_listitem);
					pullToRefreshListView.setAdapter(listViewTweetAdapter);
					listViewTweetAdapter.notifyDataSetChanged();
					progressView.setVisibility(View.GONE);
					break;
				}
				return false;
			}
		});
		initData();
	}
	public void initData(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					TweetList listOfTweet=appContext.getTweetList(uid,1, true);
					if(tweetList!=null){
						tweetList=listOfTweet.getTweetlist();
					}else{
						Toast.makeText(BigPaperViewActivity.this,R.string.null_data,Toast.LENGTH_SHORT).show();
					}
					handler.sendEmptyMessage(1);
				} catch (AppException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					handler.sendEmptyMessage(MESSAGE_EXCEPTION);
				}
				
			}
		}).start();
	}
}
